<?php
/*******************************************************************************

    FinalsClub.org is a platform for students and faculty to collaborate
    and share their knowledge with the world.

    Copyright (C) 2009  Andrew Magliozzi

    This file is a part of FinalsClub.org.

    FinalsClub.org is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    To contact FinalsClub.org, send an email to info@finalsclub.org, or
    send mail to Finals Club, c/o Veritas Tutors, 1132 Massachusetts Avenue,
    Cambridge, MA 02138.

*******************************************************************************/

/**
 * task_lists actions.
 *
 * @package    FinalsClub
 * @subpackage task_lists
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class task_listsActions extends sfActions {
	
	public function executeIndex(sfWebRequest $request)
	{
		$this->task_list_list = TaskListPeer::doSelect(new Criteria());
	}

	public function executeNew(sfWebRequest $request) {
		$this->form = new TaskListForm(null, array('group_id' => $request->getParameter('group_id')));
		$this->group = GroupPeer::retrieveById($request->getParameter('group_id'));
		
		if($request->getParameter('isAjax')) {
			$this->setTemplate('subform');
			$this->setLayout(false);
		}
	}

	public function executeCreate(sfWebRequest $request) {
		$this->forward404Unless($request->isMethod('post'));
		$this->form = new TaskListForm(null, array('group_id' => $request->getParameter('group_id')));

		$taskList = $request->getParameter($this->form->getName());
		$taskList['user_id'] = $this->getUser()->getId();
		$taskList['group_id'] = $request->getParameter('group_id');
		$taskList['type'] = TaskListPeer::$taskTypes[$taskList['type_id']];
		$request->setParameter($this->form->getName(), $taskList);
		$this->group = GroupPeer::retrieveById($request->getParameter('group_id'));
		$task_list = $this->processForm($request, $this->form);
		
		
		if($request->getParameter('isAjax')) {
			$this->setTemplate('info');
			$this->setLayout(false);
			$this->task = $task;
		}
		else
		{
			$this->redirect('tasks/new?group_id='.$task_list->getGroupId().'&task_list_id='.$task_list->getId());
		}
	}

	public function executeEdit(sfWebRequest $request) {
		$this->forward404Unless($task_list = TaskListPeer::retrieveByPk($request->getParameter('id')), sprintf('Object task_list does not exist (%s).', $request->getParameter('id')));
		$this->group = $task_list->getGroup();
		$this->form = new TaskListForm($task_list, array('group_id' => $task_list->getGroupId()));
	
		if($request->getParameter('isAjax')) {
			$this->setTemplate('subform', 'task_lists');
			$this->setLayout(false);
		}
	}

	public function executeUpdate(sfWebRequest $request)
	{
		$this->forward404Unless($request->isMethod('post') || $request->isMethod('put'));
		$this->forward404Unless($task_list = TaskListPeer::retrieveByPk($request->getParameter('id')), sprintf('Object task_list does not exist (%s).', $request->getParameter('id')));
		$this->form = new TaskListForm($task_list, array('group_id' => $task_list->getGroupId()));
		$this->group = $task_list->getGroup();
		$task_list = $this->processForm($request, $this->form);

		if($request->getParameter('isAjax')) {
			$this->setTemplate('info');
			$this->setLayout(false);
			$this->task_list = $task_list;
		}
		else
		{
			$this->redirect('tasks/index?group_id='.$task_list->getGroupId());
		}
	}

	public function executeDelete(sfWebRequest $request) {
		$request->checkCSRFProtection();

		$this->forward404Unless($task_list = TaskListPeer::retrieveByPk($request->getParameter('id')), sprintf('Object task_list does not exist (%s).', $request->getParameter('id')));
		if($task_list->getUserId() == $this->getUser()->getId() ||
			$this->getUser()->isGroupAdmin($task_list->getGroupId()) || 
			$this->getUser()->isGroupContributor($task_list->getGroupId())) {
			$task_list->delete();
			$this->getUser()->setFlash('success', 'Your task list has been successfully deleted.');
		} else {
			$this->getUser()->setFlash('error', 'You do not have permission to delete this task list.');			
		}

		$this->redirect('tasks/index?group_id='.$task_list->getGroupId());
	}
	

	protected function processForm(sfWebRequest $request, sfForm $form) {
		$form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
		if ($form->isValid()) {
			$status = $form->getObject()->isNew() ? 'created' : 'updated';
			$this->getUser()->setFlash('success', 'Your task list has been successfully '.$status);
			$task_list = $form->updateObject();
			$task_list->setType(TaskListPeer::$taskTypes[$task_list->getTypeId()]);			
			$task_list->save();
			// update all associated tasks to the current task type
			$selectCriteria = new Criteria();
			$selectCriteria->add(TaskPeer::TASK_LIST_ID, $task_list->getId());
			$updateValues = new Criteria();
			$updateValues->add(TaskPeer::TYPE, $task_list->getType());
			$updateValues->add(TaskPeer::TYPE_ID, $task_list->getTypeId());
			BasePeer::doUpdate($selectCriteria, $updateValues, Propel::getConnection());
			return $task_list;
		}
	}
}
